//
//  YZMSuggestionListCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/5.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMSuggestionListCell.h"
#import "YZMSuggestionModel.h"
#import <NSDate+DateTools.h>

static const float suggestion_cell_for_image_width = 24.0;
static const float suggestion_cell_for_margin_left = 10.0;
static const float suggestion_cell_for_margin_right = 15.0;

@implementation YZMSuggestionListCell{
    
    UIView *_bgView;
    
    UIImageView *_questionIV;
    UILabel *_question;
    UILabel *_questionTime;
    
    UIImageView *_answerIV;
    UILabel *_answer;
    UILabel *_answerTime;
    
    UIView *_splitLine;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 5.0;
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.borderColor = [YZMAppStyle colorForLine].CGColor;
        _bgView.layer.borderWidth = 1;
        [self.contentView addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.width.mas_equalTo(self.contentView).offset(-2*15);
            maker.centerX.equalTo(self.contentView);
        }];
        
        _questionIV = [UIImageView new];
        _questionIV.image = [UIImage imageNamed:@"icon_suggestion_question"];
        [_bgView addSubview:_questionIV];
        [_questionIV mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.left.top.equalTo(_bgView).offset(15);
            maker.width.height.mas_equalTo(suggestion_cell_for_image_width);
        }];
        
        _question = [YZMAppStyle labelWithMediumGray];
        _question.numberOfLines = 0;
        [_bgView addSubview:_question];
        [_question mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(_questionIV);
            maker.left.equalTo(_questionIV.mas_right).offset(suggestion_cell_for_margin_left);
            maker.right.equalTo(_bgView).offset(-suggestion_cell_for_margin_right);
        }];
        
        _questionTime = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:10] andColor:[YZMAppStyle colorForGrayUnable]];
        [_bgView addSubview:_questionTime];
        [_questionTime mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.left.right.equalTo(_question);
            maker.top.equalTo(_question.mas_bottom).offset(6);
        }];
        
        _splitLine = [UIView new];
        _splitLine.backgroundColor = [YZMAppStyle colorForLine];
        [_bgView addSubview:_splitLine];
        [_splitLine mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(_questionTime.mas_bottom).offset(15);
            maker.left.equalTo(_bgView).offset(10);
            maker.right.equalTo(_bgView).offset(-10);
            maker.height.mas_equalTo(0.5);
            
        }];
        
        _answerIV = [UIImageView new];
        _answerIV.image = [UIImage imageNamed:@"icon_suggestion_answer"];
        [_bgView addSubview:_answerIV];
        [_answerIV mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.left.equalTo(_questionIV);
            maker.top.equalTo(_splitLine.mas_bottom).offset(15);
            maker.width.height.mas_equalTo(suggestion_cell_for_image_width);
        }];
        
        _answer = [YZMAppStyle labelWithMediumGray];
        _answer.numberOfLines = 0;
        [_bgView addSubview:_answer];
        [_answer mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(_answerIV);
            maker.left.right.equalTo(_question);
        }];
        
        _answerTime = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:10] andColor:[YZMAppStyle colorForGrayUnable]];
        [_bgView addSubview:_answerTime];
        [_answerTime mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.left.right.equalTo(_question);
            maker.top.equalTo(_answer.mas_bottom).offset(6);
        }];
    }
    
    return self;
}

- (void)setRecord:(YZMSuggestionModel *)record{
    
    _question.text = record.message;
    _questionTime.text = [record.messageTime formattedDateWithFormat:@"yyyy-MM-dd HH:mm"];
    
    if (record.replyFlag) {
    
        _splitLine.hidden = NO;
        _answerIV.hidden = NO;
        _answer.hidden = NO;
        _answer.text = record.reply;
        _answerTime.hidden = NO;
        _answerTime.text = [record.replyTime formattedDateWithFormat:@"yyyy-MM-dd HH:mm"];
        
        [_bgView mas_remakeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(self.contentView);
            maker.width.mas_equalTo(self.contentView).offset(-2*15);
            maker.centerX.equalTo(self.contentView);
            maker.bottom.equalTo(_answerTime.mas_bottom).offset(15);
        }];
        
    }else{
        
        _splitLine.hidden = YES;
        _answerIV.hidden = YES;
        _answer.hidden = YES;
        _answerTime.hidden = YES;
        
        [_bgView mas_remakeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(self.contentView);
            maker.width.mas_equalTo(self.contentView).offset(-2*15);
            maker.centerX.equalTo(self.contentView);
            maker.bottom.equalTo(_questionTime.mas_bottom).offset(15);
        }];
    }
}

+ (CGFloat)heightForRecord:(YZMSuggestionModel *)record{
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    size.width -= 2*15;
    
    UIView *view = [UIView new];
    [view mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.width.mas_equalTo(size.width);
    }];
    
    UILabel *messageLabel = [UILabel new];
    messageLabel.text = record.message;
    messageLabel.font = [YZMAppStyle mediumSizeFont];
    messageLabel.numberOfLines = 0;
    [view addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(view).offset(15);
        maker.left.equalTo(view).offset(15 + suggestion_cell_for_image_width + suggestion_cell_for_margin_left);
        maker.right.equalTo(view).offset(-suggestion_cell_for_margin_right);
    }];
    
    UILabel *timeLabel = [UILabel new];
    timeLabel.text = [record.messageTime formattedDateWithFormat:@"yyyy-MM-dd"];
    timeLabel.font = [YZMAppStyle fontWithSize:10];
    [view addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.equalTo(messageLabel);
        maker.top.equalTo(messageLabel.mas_bottom).offset(6);
    }];
    
    UIView *lastView = timeLabel;
    
    if (record.replyFlag) {
    
        UILabel *messageLabel2 = [UILabel new];
        messageLabel2.text = record.reply;
        messageLabel2.font = [YZMAppStyle mediumSizeFont];
        messageLabel2.numberOfLines = 0;
        [view addSubview:messageLabel2];
        [messageLabel2 mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(timeLabel.mas_bottom).offset(2*15+1);
            maker.left.right.equalTo(messageLabel);
        }];
        
        UILabel *timeLabel2 = [UILabel new];
        timeLabel2.text = [record.replyTime formattedDateWithFormat:@"yyyy-MM-dd"];
        timeLabel2.font = [YZMAppStyle fontWithSize:10];
        [view addSubview:timeLabel2];
        [timeLabel2 mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.left.right.equalTo(messageLabel2);
            maker.top.equalTo(messageLabel2.mas_bottom).offset(6);
        }];
        
        lastView = timeLabel2;
    }
    
    [lastView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.bottom.equalTo(view).offset(-15);
    }];
    
    return [view systemLayoutSizeFittingSize:size].height;
}

@end
